package org.itstack.demo.netty.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
import org.itstack.demo.netty.msg.Request;
import org.itstack.demo.netty.msg.Response;

public class MyServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object obj) throws Exception {
        Request msg = (Request) obj;
        Response response = new Response();
        response.setRequestId(msg.getRequestId());
        response.setParam(msg.getResult() + "请求成功，反馈结果请接收处理");
        ctx.writeAndFlush(response);
        // 释放
        // 从InBound里读取的ByteBuf要手动释放
        ReferenceCountUtil.release(msg);

    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }
}
